package com.boesch.electroOffice.dataaccess.mapper;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

import com.boesch.electroOffice.dataaccess.common.IllegalDataAccessException;
import com.boesch.electroOffice.dataaccess.dao.IdentifierDao;
import com.boesch.electroOffice.model.electroOffice.Tool;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;

/**
 * This class mappes the rows from database table e_tools or e_toolsHistory to a Tools object.
 * 
 * @author Ralph Koster
 *
 */
public class ToolsMapper implements RowMapper<Tool>, Serializable {

	private static final long serialVersionUID = -5742598357903875551L;
	private static final Log log = LogFactoryUtil.getLog(ToolsMapper.class);

	private IdentifierDao identifierDao;

	public ToolsMapper(IdentifierDao identifierDao) {
		this.identifierDao = identifierDao;
	}

	/**
	 * Mapps all rows from database to a Tools object.
	 * 
	 * @see org.springframework.jdbc.core.RowMapper
	 */
	public Tool mapRow(ResultSet rs, int rowNum) throws SQLException {
		Tool tool = new Tool();
		try {
			tool.setToolIdentifier(identifierDao.getIdentifierById(rs.getInt("ToolIdentifierId")));
			tool.setName(rs.getString("Name"));
			tool.setType(rs.getString("Type"));
			tool.setDescription(rs.getString("Description"));
			tool.setManufacturer(rs.getString("Manufacturer"));
			tool.setInternalNumber(rs.getString("InternalNumber"));
		} catch (IllegalDataAccessException e) {
			log.error("Failed to load identifier with id: " + rs.getInt("ToolIdentifierId"), e);
			throw new SQLException("Failed to load identifier!", e);
		}
		return tool;
	}
}
